/**
 * Created by ajhin on 7/11/2015.
 */
package com.societyGames2.convert
{
import org.as3commons.asblocks.parser.antlr.LinkedListTree;
import org.as3commons.asblocks.parser.antlr.as3.AS3Parser;

/*
 +++INTERFACE_DEF
 ++++MODIFIERS:
 ++++IDENT: IMonkey
 ++++EXTENDS (optional)
 +++++TYPE
 ++++++IDENT: IAction
 ++++TYPE_BLOCK: {
 }
 */
public class InterfaceDefTreeConverter implements ITreeConverter
{
  private var _typeBlockTreeConverter:AfterChildTreeConverter = new AfterChildTreeConverter("", "", ";\n", true);

  //ITreeToCSharp-->
  public function convert(tree:LinkedListTree):String
  {
    var modifiersTree:LinkedListTree;
    var identityTree:LinkedListTree;
    var baseInterfaceIdentities:Vector.<String> = new Vector.<String>();
    var typeBlockTree:LinkedListTree;

    var i:int = 0;
    for (i = 0; tree.children != null && i < tree.children.length; i++)
    {
      var childTree:LinkedListTree = LinkedListTree(tree.getChild(i));
      switch (childTree.type)
      {
        case AS3Parser.MODIFIERS:
          modifiersTree = childTree;
          break;
        case AS3Parser.IDENT:
          identityTree = childTree;
          break;
        case AS3Parser.EXTENDS:
          baseInterfaceIdentities.push(TreeConverters.convert(childTree));
          break;
        case AS3Parser.TYPE_BLOCK:
          typeBlockTree = childTree;
          break;
      }
    }

    var source:String = ""; //TreeConverters.getTabs();
    var modifiersSource:String = TreeConverters.convert(modifiersTree);
    if (modifiersSource.length > 0)
    {
      source += modifiersSource + " ";
    }
    source += "interface " + TreeConverters.convert(identityTree);
    if (baseInterfaceIdentities.length > 0)
    {
      source += " : ";
      for (var baseTypeIndex:int = 0; baseTypeIndex < baseInterfaceIdentities.length; baseTypeIndex++)
      {
        if (baseTypeIndex > 0)
        {
          source += ", ";
        }
        source += baseInterfaceIdentities[baseTypeIndex];
      }
    }
    source += "\n" + /*TreeConverters.getTabs() +*/ "{\n" + _typeBlockTreeConverter.convert(typeBlockTree) + "\n" + TreeConverters.getTabs() + "}";
    return source;
  }
  //<--ITreeToCSharp
}
}
